package com.haynes.meetxiaohai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haynes.meetxiaohai.common.R;
import com.haynes.meetxiaohai.entity.ShoppingCart;
import com.haynes.meetxiaohai.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author Haynes
 * @Date 2023/3/26 15:11
 * @PackageName:com.haynes.reggie.controller
 * @ClassName: ShoppingCartController
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(HttpSession session, @RequestBody ShoppingCart shoppingCart) {
        log.info("添加的菜品数据为：" + shoppingCart.toString());

//        设置当前购物的用户ID
        Long uid = (Long) session.getAttribute("user");
        shoppingCart.setUserId(uid);

//      创建条件构造器
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, uid);

//        查询菜品或套餐是否在购物车内
        if (shoppingCart.getDishId() != null) {
//            菜品ID不为空则代表是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (shoppingCartOne != null) {
//                不为空则说明当前数据库中有该菜品，数量+1
            shoppingCartOne.setNumber(shoppingCartOne.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartOne);
        } else {
//                为空则新增一份进入数据库
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        }
        return R.success(shoppingCartOne);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session) {
        Long uid = (Long) session.getAttribute("user");

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, uid);
        shoppingCartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(shoppingCartList);
    }

    @PostMapping("/sub")
    public R<String> sub(HttpSession session, @RequestBody ShoppingCart shoppingCart) {
        Long uid = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, uid);

        if (shoppingCart.getDishId() != null) {
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lambdaQueryWrapper);
        if (shoppingCartOne != null) {
            if (shoppingCartOne.getNumber() < 1) {
                shoppingCartService.removeById(shoppingCartOne);
            }
            shoppingCartOne.setNumber(shoppingCartOne.getNumber() - 1);
            shoppingCartService.updateById(shoppingCartOne);
            return R.success("");
        }
        return R.error("删减库存失败~");
    }

    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session) {
        shoppingCartService.clean(session);
        return R.success("清空购物车成功！");
    }
}
